/*
 * NCSTCPServer.h
 *
 *  Created on: Sep 10, 2017
 *      Author: zjf
 */

#ifndef SRC_TCPSERVERWRAPPER_H_
#define SRC_TCPSERVERWRAPPER_H_

#include <map>
#include <Poco/Types.h>
#include <Poco/Net/TCPServer.h>
#include <Poco/NotificationQueue.h>

using namespace std;
using namespace Poco;
using namespace Poco::Net;

#include "TCPServerConnectionFactoryWrapper.h"
#include "TCPServerPacket.h"
#include "TCPServerPacketReaderWriter.h"

namespace network
{

class TCPServerWrapper
{
public:
	TCPServerWrapper();
	TCPServerWrapper(UInt16 port);
	virtual ~TCPServerWrapper();

	bool start(UInt16 port = nInvalidPortNumber);

	bool stop();

	TCPServerChannel::Ptr registChannel(std::string& name, std::function<void(const TCPServerDispatcherPacket::Ptr&)> handlePacket);

	void unregistChannel(std::string& name);

	static const UInt16 nInvalidPortNumber = (UInt16)-1;
private:
	UInt16 _nPort = nInvalidPortNumber;
	TCPServer* _poTCPServer = nullptr;
	std::vector<TCPServerChannel::Ptr> _vecChannels;
	std::map<std::string, int> _mapChannelIDs;

	UInt32 getFreeChannelID();
};

} /* namespace network */

#endif /* SRC_TCPSERVERWRAPPER_H_ */
